﻿using MaterialDesignThemes.Wpf;
using project.B12.application.Ioc;
using project.B12.Model.Res;
using project.B12.ViewModels;
using software.Common.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace project.B12.application.ViewModels.Dialogs
{
    public class ProjectRuleSettingViewModel : ObservableObject
    {
        public PublicResourceViewModel PublicResource { get; set; }

        public List<string> VerticalLevels { get; set; }
        public List<string> HorizontalLevels { get; set; }

        private bool _currentNegative;
        private bool _currentPositive;

        private bool isBatch { get; set; }
        /// <summary>
        /// 是否批量
        /// </summary>
        public bool IsBatch
        {
            get { return isBatch; }
            set
            {
                isBatch = value;
                this.RaisePropertyChanged();
            }
        }

        private string tip { get; set; }
        public string Tip
        {
            get { return tip; }
            set
            {
                tip = value;
                this.RaisePropertyChanged();
            }
        }

        private ObservableCollection<string> _projectMedicineNames { get; set; }
        public ObservableCollection<string> ProjectMedicineNames
        {
            get { return this._projectMedicineNames; }
            set
            {
                _projectMedicineNames = value;
                this.RaisePropertyChanged();
            }
        }

        private string projectMedicineName { get; set; }
        public string ProjectMedicineName
        {
            get { return projectMedicineName; }
            set
            {
                projectMedicineName = value;
                this.RaisePropertyChanged();
            }
        }

        private string medicineNameInput { get; set; }
        public string MedicineNameInput
        {
            get { return medicineNameInput; }
            set
            {
                medicineNameInput = value;
                if (string.IsNullOrEmpty(value))
                    ProjectMedicineNames = PublicResource.ProjectMedicineNames;
                else
                    ProjectMedicineNames = new ObservableCollection<string>(PublicResource.ProjectMedicineNames.Where(o => o.Contains(value)).ToList());
                this.RaisePropertyChanged();
            }
        }

        private string medicineName { get; set; }
        public string MedicineName
        {
            get { return medicineName; }
            set
            {
                medicineName = value;
                ProjectMedicineName = PublicResource.ProjectMedicineNames.FirstOrDefault(o => o.StartsWith(medicineName + " "));
                this.RaisePropertyChanged();
            }
        }

        private string maxConcentration { get; set; }
        public string MaxConcentration
        {
            get => maxConcentration;
            set
            {
                maxConcentration = value;
                this.RaisePropertyChanged();
            }
        }

        private List<string> concentrationLevels { get; set; }
        public List<string> ConcentrationLevels
        {
            get { return concentrationLevels; }
            set
            {
                concentrationLevels = value;
                this.RaisePropertyChanged();
            }
        }

        private string concentrationLevel { get; set; }
        public string ConcentrationLevel
        {
            get { return concentrationLevel; }
            set
            {
                concentrationLevel = value;
                this.RaisePropertyChanged();
            }
        }

        private int levelIndex = 0;
        public int LevelIndex
        {
            get { return levelIndex; }
            set
            {
                levelIndex = value;
                this.RaisePropertyChanged();
            }
        }

        private bool horizontal { get; set; }
        public bool Horizontal
        {
            get { return horizontal; }
            set
            {
                horizontal = value;
                if (horizontal)
                {
                    ConcentrationLevels = HorizontalLevels;
                    ConcentrationLevel = HorizontalLevels[0];
                }
                this.RaisePropertyChanged();
            }
        }

        private bool vertical { get; set; }
        public bool Vertical
        {
            get { return vertical; }
            set
            {
                vertical = value;
                if (vertical)
                {
                    ConcentrationLevels = VerticalLevels;
                    ConcentrationLevel = VerticalLevels[0];
                }
                this.RaisePropertyChanged();
            }
        }

        private bool isSettingEnable;
        public bool IsSettingEnable
        {
            get { return isSettingEnable; }
            set
            {
                isSettingEnable = value;
                this.RaisePropertyChanged();
            }
        }

        private bool isGrowthEnable;
        public bool IsGrowthEnable
        {
            get { return isGrowthEnable; }
            set
            {
                isGrowthEnable = value;
                this.RaisePropertyChanged();
            }
        }

        private bool negative { get; set; }
        /// <summary>
        /// 阴性
        /// </summary>
        public bool Negative
        {
            get { return negative; }
            set
            {
                negative = !_currentNegative;
                _currentNegative = negative;
                if(negative|| positive)
                {
                    IsSettingEnable = false;
                    //IsGrowthEnable = true;
                }
                else
                {
                    IsSettingEnable = true;
                    //IsGrowthEnable = false;
                }
                this.RaisePropertyChanged();
            }
        }

        private bool positive { get; set; }
        /// <summary>
        /// 阳性
        /// </summary>
        public bool Positive
        {
            get { return positive; }
            set
            {
                positive = !_currentPositive;
                _currentPositive = positive;
                if (negative || positive)
                {
                    IsSettingEnable = false;
                    //IsGrowthEnable = true;
                }
                else
                {
                    IsSettingEnable = true;
                    //IsGrowthEnable = false;
                }
                this.RaisePropertyChanged();
            }
        }

        public string DataArrangement { get; set; }

        public string GrowthControl { get; set; }

        public event Action<ProjectRuleSettingViewModel> ReturnPlate;

        public RelayCommand Save => new RelayCommand(SaveRule);

        public RelayCommand Close => new RelayCommand(Cancel);

        public RelayCommand Resetting => new RelayCommand(Clean);


        public ProjectRuleSettingViewModel()
        {
            PublicResource = ViewModelLocator.PublicResourceViewModel;
            concentrationLevels = new List<string>();
            VerticalLevels = new List<string>();
            HorizontalLevels = new List<string>();
            isBatch = false;
            IsGrowthEnable = true;
            IsSettingEnable = true;
            ProjectMedicineNames =PublicResource.ProjectMedicineNames;
        }

        private void SaveRule()
        {
            if (!CheckRule())
                return;

            ReturnPlate.Invoke(this);
        }

        private bool CheckRule()
        {
            if(Negative||Positive)
                return true;

            if (string.IsNullOrEmpty(ProjectMedicineName))
            {
                Tip = Resources.MedicineNullTip;
                return false;
            }
            if (string.IsNullOrEmpty(MaxConcentration))
            {
                Tip = Resources.ConcentrationNUllTip;
                return false;
            }
            if (IsBatch)
            {
                if (string.IsNullOrEmpty(ConcentrationLevel))
                {
                    Tip = Resources.ConcentrationLevelNullTip;
                    return false;
                }
                if (!Horizontal && !Vertical)
                {
                    Tip = Resources.DataArrangementNullTip;
                    return false;
                }
            }
            return true;
        }

        private void Cancel()
        {
            if (DialogHost.IsDialogOpen("Main"))
                DialogHost.Close("Main", true);
        }

        private void Clean()
        {
            MessageBoxResult result = MessageBox.Show("该操作会重置当前规则信息，是否继续？","提示",MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.Cancel)
                return;
            ProjectMedicineName = null;
            MaxConcentration = null;
            ConcentrationLevel = null;
            _currentPositive = true;
            _currentNegative = true;
            Negative = false;
            Positive=false;
        }
    }
}
